package com.zhouyibike.uu.ui.activity.mywallet;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MydetailResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.adapter.MydetailAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;
import com.zhouyibike.uu.utils.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyDetailActivity extends BaseActivity implements View.OnClickListener{
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private LinearLayout tv_moretype;
    private TextView tv_type;
    private TextView tv_alltype,tv_cz,tv_dcsy,tv_fxsy,tv_qc,tv_tx,tv_rgdc,tv_gmyq,tv_yiquanzhuanrang,tv_yihuodongrengou,tv_lqhb,tv_zzyq,tv_hdzzyq,tv_yqxf,tv_yqtx,tv_cssy,tv_cstx;

    private LinearLayout ll_nothing;
    private PullToRefreshLayout pullToRefreshLayout;
    private ListView lv_allmsg;
    private int  refreshtype=0;
    private int page=1;//页码
    private boolean ismore=true;//判断是否还有更多数据
    private List<MydetailResult.DataBean> alllist=new ArrayList<>();
    private MydetailAdapter mydetailAdapter;
    private String type="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_detail);
        type=getIntent().getStringExtra("type");
        initView();
        getMsg();
    }
    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("我的明细");
        mBtnBack.setOnClickListener(this);
        tv_moretype= (LinearLayout) findViewById(R.id.tv_bar_moretype);
        tv_type= (TextView) findViewById(R.id.tv_moretype_text);
        tv_moretype.setVisibility(View.VISIBLE);
        if(type.equals("0")){
            tv_type.setText("全部明细");
        }else if(type.equals("2")){
            tv_type.setText("单车收益");
        }else if(type.equals("1")){
            tv_type.setText("余额充值");
        }else if(type.equals("13")){
            tv_type.setText("收益提现");
        }if(type.equals("30")){
            tv_type.setText("获得转账翼券");
        }
        tv_moretype.setOnClickListener(this);

        pullToRefreshLayout=((PullToRefreshLayout) findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new MyListener2());
        ll_nothing= (LinearLayout) findViewById(R.id.ll_nothing_show);
        lv_allmsg=(ListView) findViewById(R.id.content_view);
    }
    private void getMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("pageIndex", page+"");
        params.put("type",type);
        if(page==1&&refreshtype==0)
            showProgressDialog();
        addSubscription(apiStores.GetOrders(params),
                new ApiCallback<MydetailResult>() {

                    @Override
                    public void onSuccess(MydetailResult model) {
                                                String string=new Gson().toJson(model);
                        Log.e("onSuccess: ",string );
                        if(model.getStatus()== Config.SUCCESSCODE){
                            if( refreshtype==1){
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            }else if( refreshtype==2){
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if(model.getData().size()==0){
                                ismore=false;
                            }
                            alllist.addAll(model.getData());
                            if(alllist.size()==0){
                                pullToRefreshLayout.setVisibility(View.GONE);
                                ll_nothing.setVisibility(View.VISIBLE);
                            }else{
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                ll_nothing.setVisibility(View.GONE);
                            }
                            initData();

                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    private void initData(){
        if(page==1) {
            mydetailAdapter = new MydetailAdapter(this, alllist);
            lv_allmsg.setAdapter(mydetailAdapter);
        }else{
            mydetailAdapter.setList(alllist);
        }
    }
    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype=1;
            page=1;
            ismore=true;
            getMsg();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype=2;
            if(ismore) {
                page++;
                getMsg();
            }else{
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.tv_bar_moretype:
                showpop(v);
                backgroundAlpha(0.6f);
                break;
            case R.id.tv_alltype_all:
                popupWindow.dismiss();
                tv_type.setText("全部明细");
                alllist.clear();
                page=1;
                type="0";
                getMsg();
                break;
            case R.id.tv_alltype_cz:
                popupWindow.dismiss();
                tv_type.setText("余额充值");
                alllist.clear();
                page=1;
                type="1";
                getMsg();
                break;
            case R.id.tv_alltype_dcsy:
                popupWindow.dismiss();
                tv_type.setText("单车收益");
                alllist.clear();
                type="2";
                page=1;
                getMsg();
                break;
            case R.id.tv_alltype_fxsy:
                popupWindow.dismiss();
                tv_type.setText("分享收益");
                alllist.clear();
                page=1;
                type="3";
                getMsg();
                break;
            case R.id.tv_alltype_qcsy:
                popupWindow.dismiss();
                tv_type.setText("骑车消费");
                alllist.clear();
                page=1;
                type="12";
                getMsg();
                break;
            case R.id.tv_alltype_rgdc:
                popupWindow.dismiss();
                tv_type.setText("认购单车");
                alllist.clear();
                page=1;
                type="11";
                getMsg();
                break;
            case R.id.tv_alltype_tixian:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("收益提现");
                alllist.clear();
                type="13";
                getMsg();
                break;
            case R.id.tv_alltype_goumaiyiquan:
                popupWindow.dismiss();
                tv_type.setText("购买翼券");
                alllist.clear();
                page=1;
                type="14";
                getMsg();
                break;
            case R.id.tv_alltype_yiquanzhuanrangshouyi:
                popupWindow.dismiss();
                tv_type.setText("翼券转让收益");
                alllist.clear();
                page=1;
                type="6";
                getMsg();
                break;
            case R.id.tv_alltype_yihuodongrengoudanche:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("翼活动认购单车");
                alllist.clear();
                type="15";
                getMsg();
                break;
            case R.id.tv_alltype_lqhb:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("领取红包");
                alllist.clear();
                type="10";
                getMsg();
                break;
            case R.id.tv_alltype_yqzz:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("翼券转账");
                alllist.clear();
                type="20";
                getMsg();
                break;
            case R.id.tv_alltype_hdzzyq:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("获得转账翼券");
                alllist.clear();
                type="30";
                getMsg();
                break;
            case R.id.tv_alltype_yqxf:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("翼券消费");
                alllist.clear();
                type="23";
                getMsg();
                break;
            case R.id.tv_alltype_yiquansytx:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("翼券收益提现");
                alllist.clear();
                type="21";
                getMsg();
                break;
            case R.id.tv_alltype_csgmdcsy:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("城市购买单车收益");
                alllist.clear();
                type="31";
                getMsg();
                break;
            case R.id.tv_alltype_cssytx:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("城市收益提现");
                alllist.clear();
                type="22";
                getMsg();
                break;
            case R.id.tv_alltype_csgmyqsy:
                popupWindow.dismiss();
                page=1;
                tv_type.setText("城市翼券消费收益");
                alllist.clear();
                type="32";
                getMsg();
                break;
//            tv_yqxf= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yqxf);
//            tv_yqxf.setOnClickListener(this);
//            tv_yqtx= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yiquansytx);
//            tv_yqtx.setOnClickListener(this);
//            tv_cssy= (TextView) popupWindowView.findViewById(R.id.tv_alltype_csgmdcsy);
//            tv_cssy.setOnClickListener(this);
//            tv_cstx= (TextView) popupWindowView.findViewById(R.id.tv_alltype_cssytx);
//            tv_cstx.setOnClickListener(this);
        }
    }
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
    PopupWindow popupWindow;
    private void showpop(View view){
        View popupWindowView = getLayoutInflater().inflate(R.layout.pop_yydc_success, null);
         popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);

        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        int pos = width - popupWindow.getWidth();
        popupWindow.showAsDropDown(this.findViewById(R.id.view_titlebar_line), pos, (0-Util.dp2px(this,4)));
        tv_alltype= (TextView) popupWindowView.findViewById(R.id.tv_alltype_all);
        tv_alltype.setOnClickListener(this);
        tv_cz= (TextView) popupWindowView.findViewById(R.id.tv_alltype_cz);
        tv_cz.setOnClickListener(this);
        tv_rgdc= (TextView) popupWindowView.findViewById(R.id.tv_alltype_rgdc);
        tv_rgdc.setOnClickListener(this);
        tv_dcsy= (TextView) popupWindowView.findViewById(R.id.tv_alltype_dcsy);
        tv_dcsy.setOnClickListener(this);
        tv_fxsy= (TextView) popupWindowView.findViewById(R.id.tv_alltype_fxsy);
        tv_fxsy.setOnClickListener(this);
        tv_qc= (TextView) popupWindowView.findViewById(R.id.tv_alltype_qcsy);
        tv_qc.setOnClickListener(this);
        tv_tx= (TextView) popupWindowView.findViewById(R.id.tv_alltype_tixian);
        tv_tx.setOnClickListener(this);
        tv_gmyq= (TextView) popupWindowView.findViewById(R.id.tv_alltype_goumaiyiquan);
        tv_gmyq.setOnClickListener(this);
        tv_yiquanzhuanrang= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yiquanzhuanrangshouyi);
        tv_yiquanzhuanrang.setOnClickListener(this);
        tv_yihuodongrengou= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yihuodongrengoudanche);
        tv_yihuodongrengou.setOnClickListener(this);
//        tv_lqhb,tv_zzyq,tv_hdzzyq
        tv_lqhb= (TextView) popupWindowView.findViewById(R.id.tv_alltype_lqhb);
        tv_lqhb.setOnClickListener(this);
        tv_zzyq= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yqzz);
        tv_zzyq.setOnClickListener(this);
        tv_hdzzyq= (TextView) popupWindowView.findViewById(R.id.tv_alltype_hdzzyq);
        tv_hdzzyq.setOnClickListener(this);
//        tv_yqxf,tv_yqtx,tv_cssy,tv_cstx
        tv_yqxf= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yqxf);
        tv_yqxf.setOnClickListener(this);
        tv_yqtx= (TextView) popupWindowView.findViewById(R.id.tv_alltype_yiquansytx);
        tv_yqtx.setOnClickListener(this);
        tv_cssy= (TextView) popupWindowView.findViewById(R.id.tv_alltype_csgmdcsy);
        tv_cssy.setOnClickListener(this);
        tv_cstx= (TextView) popupWindowView.findViewById(R.id.tv_alltype_cssytx);
        tv_cstx.setOnClickListener(this);
        TextView tv_alltype_csgmyqsy;
        tv_alltype_csgmyqsy= (TextView)popupWindowView. findViewById(R.id.tv_alltype_csgmyqsy);
        tv_alltype_csgmyqsy.setOnClickListener(this);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }
}
